#include "T9manager.h"


time_t BeginTimer()
{
    //timer declaration
    time_t Begin; //initialize Begin

    time(&Begin); //start the timer

    return Begin;
}
time_t ElapsedTimer(time_t Begin)
{
    time_t End;
    time(&End);   //stop the timer
    return ((End-Begin));
}


char* substring_r(char* buffer, char* str, int start, int end)
{
	int i, x = 0;
	for(i = start ; i <= end; i++)
		buffer[x++] = str[i];
	buffer[x] = '\0';	
	return buffer;
}


void SendEvent(XKeyEvent *event, int press){
	if (press)
		XSendEvent(event->display, event->window, True, KeyPressMask,(XEvent *)event);
	else
		XSendEvent(event->display, event->window, True, KeyReleaseMask,(XEvent *)event);
		XSync(event->display, False);
}


int sendChar(int c,int state){

	char disp[] = ":0";
	char *dp = getenv("DISPLAY");
	if (!dp)
		dp = disp;
	Display *dpy = XOpenDisplay(dp);
	if (dpy == NULL){
		return false;
	}
	else {
		Window cur_focus; 
		int revert_to; 
		XGetInputFocus(dpy, &cur_focus, &revert_to);
		if (cur_focus == None){
			return true;
		}
		else{
			XKeyEvent event;
			event.display = dpy;
			event.window = cur_focus;
			event.root = RootWindow(event.display,
			DefaultScreen(event.display));
			event.subwindow = None;
			event.time = CurrentTime;
			event.x = 1;
			event.y = 1;
			event.x_root = 1;
			event.y_root = 1;
			event.same_screen = True;
			event.type = KeyPress;
			event.state = state;
			event.keycode = XKeysymToKeycode(dpy,c);
			SendEvent(&event, true);
			event.type = KeyRelease;
			SendEvent(&event, false);
		}
		XCloseDisplay(dpy);
	}
	return true;
}


int tobeSaved(char* str){
	int i;
	for(i=0;i<strlen(str);i++){
	if(str[i]=='0' || str[i]=='1' || str[i]=='2' || str[i]=='3' || str[i]=='4' || str[i]=='5' || str[i]=='6' || str[i]=='7' || str[i]=='8' || str[i]=='9')
		return 0;
	if(str[i]==',' || str[i]=='.' || str[i]=='-' || str[i]=='\\' || str[i]=='+' || str[i]=='<' || str[i]=='\'')
		return 0;	
		if(charToKeyCode(str[i])!=0){
			int ascii=(int)str[i];
			if(ascii>=65 && ascii<=90)
				return 1;
			else
				return 0;
		}
	}

	return 1;
}	

int charToKeyCode(char currentChar){

	int ascii=(int)currentChar;
if ((ascii>=33 && ascii<=42 && ascii!=39 && ascii!=35) || (ascii>=58 && ascii<=90 && ascii!=60 && ascii!=64) || ascii==47|| ascii==94 || ascii==95 || ascii==124 || ascii==130) {
		return 1; //Value 1 is for every symbol obtained with "Shift" pressed
	}
	if (ascii==35 || ascii==64 || ascii==91 || ascii==93) {
		return XK_Shift_R ; //Value XK_Shift_R is for every symbol obtained with "Alt gr" pressed
	}
	return 0;
}

int *FindAccentedChar(char *str,int *indici){
	int x=strlen(str);
	char *buffer=(char *)malloc(20*sizeof(char));
	int i=0;
	int j=0;
	for (i=0; i<x-1; i++) {
		buffer=substring_r(buffer,str,i,i+1);
		if ((strcmp(buffer,"à")==0)||(strcmp(buffer,"è")==0)||(strcmp(buffer,"ì")==0)||(strcmp(buffer,"ò")==0)||(strcmp(buffer,"ù")==0)) {
			indici[j++]=i;
		}
	}
	indici[j]=-1;
	return indici;


}

void *sendString(char *str){
	int x=strlen(str);
	char *buffer=(char *)malloc(wordlen*sizeof(char));
	int i=0;
	int j=0;
	for (i=0; i<=x-1; i++) {
		if(i!=(strlen(str)-1)){
			buffer=substring_r(buffer,str,i,i+1);
			if ((strcmp(buffer,"è")==0)) {
				sendChar(XK_egrave,0);
				i=i+1;
			}
			else if((strcmp(buffer,"à")==0)){
				sendChar(XK_agrave,0);
				i=i+1;
			}
			else if((strcmp(buffer,"ì")==0)){
				sendChar(XK_igrave,0);
				i=i+1;
			}
			else if((strcmp(buffer,"ò")==0)){
				sendChar(XK_ograve,0);
				i=i+1;
			}
			else if((strcmp(buffer,"ù")==0)){
				sendChar(XK_ugrave,0);
				i=i+1;
			}
			else{
			sendChar(str[i],charToKeyCode(str[i]));
			}
		}
		else if(i==(strlen(str)-1)){
			sendChar(str[(strlen(str))-1],charToKeyCode(str[i]));
		}
	}

}



char* toUpperString(char *str){
	int i;
	for(i=0;str[i];i++)
		str[i]=toupper(str[i]);
	return str;
}

char* toLowerString(char *str){
	int i;
	for(i=0;str[i];i++)
		str[i]=tolower(str[i]);
	return str;
}

int isUpperString(char *str){
	int i;
	int control;
	for(i=0;str[i];i++)
		control=isupper(str[i]);
	return control;
}



int containsAccentedChar(char *str){
	int *indici = (int*)malloc(strlen(str)*sizeof(int));
	indici=FindAccentedChar(str,indici);
	if(indici[0]==-1)
		return 0;
	else
		return 1;
}

char* invertAccentedStrings(char *str){
	int *indici = (int*)malloc(strlen(str)*sizeof(int));
	indici=FindAccentedChar(str,indici);
	int currentaccentedchar=0;
	int i;
	for(i=0;i<strlen(str);i++){
		if((isupper(str[i]))&&(i!=indici[currentaccentedchar]))
			str[i]=tolower(str[i]);
		else if((islower(str[i]))&&(i!=indici[currentaccentedchar]))
			str[i]=toupper(str[i]);
		else if(i==indici[currentaccentedchar]){	
			currentaccentedchar++;
			i++;
		}
	}
	return str;
	
}

